package com.esiag.isidis.fi.pdsTeam.jms.producer;



import java.io.Serializable;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import com.esiag.isidis.fi.pdsTeam.producer.IProducer;
import com.esiag.isidis.fi.pdsTeam.jms.connection.AbstractConnectionFactory;



public abstract class AbstractProducerFactory<T extends Serializable>  implements IProducer {
	AbstractConnectionFactory connection;
	Session session;
	public AbstractProducerFactory(@SuppressWarnings("rawtypes") Class msgClass) {
		
			try {
				connection=getConnection(msgClass);
				connection.start();
				session=connection.getSession();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public void send(T info) throws Exception{
		ObjectMessage msg;
		msg = session.createObjectMessage();
		msg.setObject(info);
		MessageProducer producer = connection.getProducer(getQueue(info.getClass()));
		producer.send(msg);
	}
	
	public void close() throws JMSException{
		connection.close();
	}
	abstract public String getQueue(@SuppressWarnings("rawtypes") Class msgClass);
	abstract public AbstractConnectionFactory getConnection(@SuppressWarnings("rawtypes") Class msgClass)throws JMSException;
}
